package com.dreams.dream.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dreams.dream.common.async.disruptor.DisruptorHelper;
import com.dreams.dream.common.async.disruptor.DomainMessage;
import com.dreams.dream.common.async.event.LogEventHandler;
import com.dreams.dream.common.async.event.LoginHisEventHandler;
import com.dreams.dream.common.async.event.OperateLogEventHandler;
import com.dreams.dream.common.async.event.Test2EventHandler;
import com.dreams.dream.common.async.event.TestEventHandler;
import com.dreams.dream.common.constant.GlobalConstant;
import com.dreams.dream.common.utils.CollectionUtils;
import com.dreams.dream.common.utils.StringUtil;
import com.dreams.dream.pojo.base.Criteria;
import com.dreams.dream.pojo.base.UserAccount;
import com.dreams.dream.service.UserAccountService;
import com.dreams.dream.service.UserInfoService;

/**
 * 登陆控制器
 * 
 * @author Lawliet
 * 
 */
@Controller
public class LoginController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    UserAccountService userAccountService;

    @Autowired
    LogEventHandler logEventHandler;
    @Autowired
    LoginHisEventHandler loginHisEventHandler;
    @Autowired
    OperateLogEventHandler operateLogEventHandler;
    @Autowired
    TestEventHandler testEventHandler;
    @Autowired
    Test2EventHandler test2EventHandler;
    
    
    /**
     * 登陆
     * 
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request) {

        // AsyncTest.testMain(null);

        long nowtime = System.currentTimeMillis();
        DisruptorHelper.publish(logEventHandler, "测试111", "log");
        DisruptorHelper.publish(loginHisEventHandler, "测试222", "loginHis");
        DisruptorHelper.publish(operateLogEventHandler, "测试333", "operateLog");
        DisruptorHelper.publish(logEventHandler, "测试444", "log");
        DisruptorHelper.publish(loginHisEventHandler, "测试555", "loginHis");
        DisruptorHelper.publish(operateLogEventHandler, "测试666", "operateLog");
        
        DomainMessage message1 = DisruptorHelper.publish(testEventHandler, "测试777", "test1");
        DomainMessage message2 = DisruptorHelper.publish(test2EventHandler, "测试888", "test2");
        DomainMessage message3 = DisruptorHelper.publish(test2EventHandler, "测试999", "test2");
        
//        message1.getBlockEventResult()
        System.out.println("message1 is ----------------------------------" + message1.getEventResult());
        System.out.println("message2 is ----------------------------------" + message2.getEventResult());
        System.out.println("message3 is ----------------------------------" + message3.getEventResult());
        System.out.println(System.currentTimeMillis() - nowtime);
        
//        
        //
        // System.out.println(getIpAddr(request));

        // 用户名
        String loginName = request.getParameter("loginName");
        // 密码
        String loginPass = request.getParameter("loginPass");

        // 直接跳到登陆画面
        if (loginName == null || loginPass == null) {
            return "redirect:login";
        }

        if (StringUtil.isEmpty(loginName) || StringUtil.isEmpty(loginPass)) {
            request.setAttribute("msg", "用户名或密码不正确,请重新输入！");
            return "login";
        }

        // 条件对象
        Criteria example = new Criteria();
        example.put("user_account", loginName);
        example.put("user_pass", loginPass);

        List<UserAccount> userAccountList = userAccountService.selectByExample(example);

        // 用户不存在或密码不正确
        if (CollectionUtils.isEmpty(userAccountList)) {
            request.setAttribute("msg", "用户名或密码不正确,请重新输入！");
            return "login";
        }

        // 取得用户对象
        UserAccount userAccount = userAccountList.get(0);

        // 判断用户是否为启动状态
        if (GlobalConstant.USE_FLAG_0.equals(userAccount.getUseFlag())) {
            request.setAttribute("msg", "你账号已禁用！");
            return "login";
        }

        // TODO 记录用户登陆历史
        // TODO 将用户信息放入session

        return "index";
    }

    /**
     * 取得客户端真实ip
     * 
     * @param request
     * @return 客户端真实ip
     */
    public String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        logger.debug("1- X-Forwarded-For ip={}", ip);
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            logger.debug("2- Proxy-Client-IP ip={}", ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
            logger.debug("3- WL-Proxy-Client-IP ip={}", ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
            logger.debug("4- HTTP_CLIENT_IP ip={}", ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            logger.debug("5- HTTP_X_FORWARDED_FOR ip={}", ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            logger.debug("6- getRemoteAddr ip={}", ip);
        }
        logger.info("finally ip={}", ip);
        return ip;
    }

}
